//
//  LoginButtonVM.swift
//  kxf
//
//  Created by Mac on 2024/3/4.
//

import Foundation
import UIKit

class LoginButtonVM: UIView {
    
    let selfHeight = kFitWidth(120)
    
    var registBlock:(()->())?
    var forgetBlock:(()->())?
    var loginBlock:(()->())?

    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var forgetBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("忘记密码", for: .normal)
        btn.setTitleColor(.COLOR_TEXT_BLACK666, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        btn.backgroundColor = .clear
        
        btn.addTarget(self, action: #selector(forgetAction), for: .touchUpInside)
        
        return btn
    }()
    lazy var loginBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.layer.cornerRadius = kFitWidth(24)
        btn.clipsToBounds = true
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .regular)
        btn.backgroundColor = .clear
        btn.setBackgroundImage(UIImage.init(named: "button_theme_bg"), for: .normal)
        
        btn.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        
        return btn
    }()
    lazy var registLabel : UILabel = {
        let lab = UILabel()
        lab.text = "还没有账号？立即注册"
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 12)
        lab.isUserInteractionEnabled = true
        
        return lab
    }()
    lazy var registArrowImg : UIImageView = {
        let img = UIImageView()
        img.image = UIImage.init(named: "login_arrow_img")
        img.isUserInteractionEnabled = true
        
        return img
    }()
    lazy var registTapView : UIView = {
        let vi = UIView()
        vi.isUserInteractionEnabled = true
        vi.backgroundColor = .clear
        
        let tap = UITapGestureRecognizer.init(target: self, action:    #selector(registeAction))
        vi.addGestureRecognizer(tap)
        
        return vi
    }()
}

extension LoginButtonVM{
    @objc func registeAction() {
        if self.registBlock != nil{
            self.registBlock!()
        }
    }
    @objc func forgetAction() {
        if self.forgetBlock != nil{
            self.forgetBlock!()
        }
    }
    @objc func loginAction() {
        if self.loginBlock != nil{
            self.loginBlock!()
        }
    }
}
extension LoginButtonVM{
    func initUI() {
        addSubview(forgetBtn)
        addSubview(loginBtn)
        addSubview(registLabel)
        addSubview(registArrowImg)
        addSubview(registTapView)
        
        setConstrait()
    }
    func setConstrait() {
        forgetBtn.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(-64))
            make.top.equalToSuperview()
            make.height.equalTo(kFitWidth(24))
        }
        loginBtn.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.width.equalTo(kFitWidth(260))
            make.height.equalTo(kFitWidth(48))
            make.top.equalTo(kFitWidth(34))
        }
        registLabel.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.top.equalTo(loginBtn.snp.bottom).offset(kFitWidth(14))
        }
        registArrowImg.snp.makeConstraints { make in
            make.left.equalTo(registLabel.snp.right)
            make.centerY.lessThanOrEqualTo(registLabel)
            make.width.height.equalTo(kFitWidth(20))
        }
        registTapView.snp.makeConstraints { make in
            make.left.equalTo(registLabel)
            make.right.equalTo(registArrowImg)
            make.top.equalTo(loginBtn.snp.bottom).offset(kFitWidth(5))
            make.bottom.equalTo(registLabel.snp.bottom).offset(kFitWidth(5))
        }
    }
}
